package com.yuncheng.spcyApi.selectXzxkApi.api;

import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SpcySqsxBczgjl;
import com.yuncheng.spcyApi.entity.SpcySqsxBqbzjl;
import com.yuncheng.spcyApi.entity.SpcySqsxJob;
import com.yuncheng.spcyApi.flow.BqbzFlowService;
import com.yuncheng.spcyApi.flow.FlowVo;
import com.yuncheng.spcyApi.flow.SpcyQyFlowService;
import com.yuncheng.spcyApi.mapper.SpcySqsxJobMapper;
import com.yuncheng.spcyApi.selectXzxkApi.service.PutZwxtService;
import com.yuncheng.spcyApi.selectXzxkApi.vo.PutBqbzVo;
import com.yuncheng.spcyApi.spcyService.SqsxQywjclService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.IpRateLimiterUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 政务服务系统 - 对接政务服务接口功能
 */
@Api(tags = "审评中心-对接政务服务接口功能")
@RestController
@RequestMapping("/api/spzx-spcy/spcy-zwfw")
public class PutXwxtSpcyApi {
    @Resource
    private PutZwxtService putZwxtService;

    @Resource
    private BqbzFlowService bqbzFlowService;

    @Resource
    private SpcyQyFlowService spcyQyFlowService;

    @Resource
    private SpcySqsxJobMapper spcySqsxJobMapper;

    @Resource
    private SqsxQywjclService sqsxQywjclService;

    @Log(title = "退回政务服务接口")
    @ApiOperation(value = "退回政务服务接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id"),
            @ApiImplicitParam(name = "thyy",value = "退回原因")
    })
    @PostMapping(value = "returnZwfwxt")
    public HttpResult returnZwfwxt(@RequestBody Map<String,String> map){
        String sqsxid = map.get("sqsxid");
        String thyy = map.get("thyy");
        boolean flag = putZwxtService.returnZwfwxt(sqsxid, thyy);
        if (!flag){
            return HttpResult.ok("退回失败");
        }

        return HttpResult.ok("退回成功");
    }

    @Log(title = "企业申请撤件-推送撤件意见")
    @ApiOperation(value = "企业申请撤件-推送撤件意见")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id"),
            @ApiImplicitParam(name = "opinion",value = "撤件意见")
    })
    @PostMapping(value = "pushQyCjOpinion")
    public HttpResult pushQyCjOpinion(@RequestBody Map<String,String> map){
        String sqsxid = map.get("sqsxid");
        String opinion = map.get("opinion");

        putZwxtService.pushSpzxCjyj(sqsxid, opinion);
        return HttpResult.ok("提交成功");
    }

    @Log(title = "推送企业补齐补正通知接口-补充信息及材料")
    @ApiOperation(value = "推送企业补齐补正通知接口-补充信息及材料")
    @PostMapping(value = "pushQyBqbzNotice")
    public HttpResult pushQyBqbzNotice(@RequestBody PutBqbzVo bqbzjl){
        putZwxtService.pushQyBqbzNotice(bqbzjl);

        return HttpResult.ok("发送成功");
    }

    /** 2023-03-26 新增 */
    @Log(title = "补齐补正-流程提交")
    @ApiOperation(value = "补齐补正-流程提交")
    @PostMapping(value = "submitFlowByBqbz")
    public HttpResult submitFlowByBqbz(@RequestBody PutBqbzVo flowVo){
        // 接口限流
        if (!IpRateLimiterUtils.tryAcquire(GetCurrentUserUtils.getCurrentUserId())){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        if (StringUtils.isBlank(flowVo.getJobid())){
            return HttpResult.error("流程任务参数丢失，请联系管理员！");
        }

        SpcySqsxJob nowJob = spcySqsxJobMapper.selectById(flowVo.getJobid());
        if (nowJob == null){
            return HttpResult.error("当前流程任务不存在，请刷新重试！");
        }

        Result httpResult = bqbzFlowService.passNextNodeFlow(flowVo, flowVo.getJobid(), nowJob, flowVo.getFsqsxid(), flowVo.getOpinion());
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        // 实时推送流转意见
        putZwxtService.pushSingletonSpJobLzjl(nowJob.getFpid());

        return HttpResult.ok("提交成功");
    }

    @Log(title = "企业端-流程提交")
    @ApiOperation(value = "企业端-流程提交")
    @PostMapping(value = "submitFlowByQy")
    public HttpResult submitFlowByQy(@RequestBody FlowVo flowVo){
        // 接口限流
        if (!IpRateLimiterUtils.tryAcquire(GetCurrentUserUtils.getCurrentUserId())){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        if (StringUtils.isBlank(flowVo.getJobid())){
            return HttpResult.error("流程任务参数丢失，请联系管理员！");
        }

        Result result = spcyQyFlowService.passJobByQy(flowVo);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    @Log(title = "获取当前补齐补正的内容")
    @ApiOperation(value = "获取当前补齐补正的内容")
    @GetMapping(value = "queryNowBqbzBean")
    public HttpResult queryNowBqbzBean(@RequestParam(value = "sqsxid",defaultValue = "OAOCSS")String sqsxid){

        return HttpResult.ok(putZwxtService.getNowBqbznr(sqsxid));
    }

    @Log(title = "更新补齐补正短信内容")
    @ApiOperation(value = "更新补齐补正短信内容")
    @PostMapping(value = "updateBqbz")
    public HttpResult updateBqbz(@RequestBody SpcySqsxBqbzjl bean){
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.ok("参数为空！");
        }

        putZwxtService.updateBqbz(bean);

        return HttpResult.ok();
    }

    @Log(title = "推送企业补充整改通知接口")
    @ApiOperation(value = "推送企业补充整改通知接口")
    @PostMapping(value = "pushQyBcZgNotice")
    public HttpResult pushQyZgNotice(@RequestBody SpcySqsxBczgjl bczgjl){

        Result result = putZwxtService.pushQyZgNotice(bczgjl);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    @Log(title = "获取当前整改通知的内容")
    @ApiOperation(value = "获取当前整改通知的内容")
    @GetMapping(value = "queryNowZgtzBean")
    public HttpResult queryNowZgtzBean(@RequestParam(value = "sqsxid",defaultValue = "OAOCSS")String sqsxid){

        return HttpResult.ok(putZwxtService.getNowZgtzContent(sqsxid));
    }

    @Log(title = "验证是否存在发起未完成的整改记录(1: 存在整改；0：不存在)")
    @ApiOperation(value = "验证是否存在发起未完成的整改记录(1: 存在整改；0：不存在)")
    @GetMapping(value = "zglszt")
    public HttpResult zglszt(@RequestParam(value = "sqsxid",defaultValue = "OAOCSS")String sqsxid){

        if (putZwxtService.verifySfExistsZgjlWithWwc(sqsxid)){
            return HttpResultVo.HttpResultGet("1");
        }

        return HttpResultVo.HttpResultGet("0");
    }


    /** 2024-02-24 新增 */
//    @ApiOperation(value = "验证是否发起补齐补正(true: 已补正/false: 未补正)")
    @GetMapping(value = "verifySffqBqbz")
    public HttpResult verifySffqBqbz(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        return HttpResult.ok(putZwxtService.verifySffqBqbz(sqsxid));
    }

    /**
     * 同步政务系统-补齐补正材料
     * */
    @GetMapping(value = "syncZwxtBqbzcl")
    public HttpResult syncZwxtBqbzcl(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                     @RequestParam(value = "sxsqjbxxid",defaultValue = "")String sxsqjbxxid){

        sqsxQywjclService.syncZwxtBqbzclToLocalWjcl(sqsxid, sxsqjbxxid);
        return HttpResult.ok();
    }

    /**
     * 同步政务系统-整改材料
     * */
    @GetMapping(value = "syncZwxtZgcl")
    public HttpResult syncZwxtZgcl(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                     @RequestParam(value = "sxsqjbxxid",defaultValue = "")String sxsqjbxxid){

        sqsxQywjclService.syncZwxtZgclToLocalWjcl(sqsxid, sxsqjbxxid);
        return HttpResult.ok();
    }

    /**
     * 同步政务系统-撤件申请材料
     * */
    @GetMapping(value = "syncZwxtQycjcl")
    public HttpResult syncZwxtQycjcl(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                   @RequestParam(value = "sxsqjbxxid",defaultValue = "")String sxsqjbxxid){

        sqsxQywjclService.syncZwxtCjsqclToLocalWjcl(sqsxid, sxsqjbxxid);
        return HttpResult.ok();
    }
}
